home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / ashc5.arc / SYMTAB.C < prev    next >
C/C++ Source or Header  |  1990-07-15  |  3KB  |  134 lines

  1. /*
  2.  *      install --- add a symbol to the table
  3.  */
  4. install(str,val)
  5. char    *str;
  6. int     val;
  7. {
  8.         struct link *lp;
  9.     struct nlist *np,*p,*backp;
  10.     struct nlist *lookup();
  11.     int     i;
  12.  
  13.     if( !alpha(*str) ){
  14.         error("Illegal Symbol Name");
  15.         return(NO);
  16.         }
  17.     if( (np = lookup(str)) != NULL ){
  18.         if( Pass==2 ){
  19.             if( np->def == val )
  20.                 return(YES);
  21.             else{
  22.                 error("Phasing Error");
  23.                 return(NO);
  24.                 }
  25.             }
  26.         error("Symbol Redefined");
  27.         return(NO);
  28.         }
  29.     /* enter new symbol */
  30. #ifdef DEBUG
  31.     printf("Installing %s as %d\n",str,val);
  32. #endif
  33.     np = (struct nlist *) alloc(sizeof(struct nlist));
  34.     np->name = alloc(strlen(str)+1);
  35.     strcpy(np->name,str);
  36.     np->def = val;
  37.     np->Lnext = NULL;  
  38.         np->Rnext = NULL;
  39.            lp = (struct link *) alloc(sizeof(struct link));
  40.            np->L_list = lp;
  41.            lp->L_num = Line_num;
  42.            lp->next = NULL;
  43.         p = root;
  44.           backp = NULL;
  45.            while (p != NULL) 
  46.             {
  47.               backp = p;
  48.               i = strcmp (str,p->name);
  49.           if (i<0)
  50.           p=p->Lnext;
  51.           else p=p->Rnext;
  52.             }
  53.           if (backp == NULL)
  54.               root = np;
  55.       else if (strcmp(str,backp->name)<0)
  56.           backp->Lnext = np;
  57.            else backp->Rnext = np;
  58.           return (YES);  
  59. }
  60.  
  61. /*
  62.  *      lookup --- find string in symbol table
  63.  */
  64. struct nlist *
  65. lookup(name)
  66. char    *name;
  67. {
  68.     struct nlist *np;
  69.     int     i;
  70.  
  71.         np = root;
  72.          while (np != NULL)
  73.           {
  74.             i = strcmp(name,np->name);
  75.              if (i == 0)
  76.                {
  77.                  Last_sym = np->def;
  78.                  return (np);
  79.                }
  80.              else if (i < 0)
  81.              np = np->Lnext;
  82.           else np = np->Rnext;
  83.           }
  84.       Last_sym = 0;
  85.       if (Pass == 2)
  86.           error ("symbol Undefined on pass 2");
  87.       return (NULL);
  88. }
  89.  
  90.  
  91. #define NMNE (sizeof(table)/ sizeof(struct oper))
  92. #define NPSE (sizeof(pseudo)/ sizeof(struct oper))
  93. /*
  94.  *      mne_look --- mnemonic lookup
  95.  *
  96.  *      Return pointer to an oper structure if found.
  97.  *      Searches both the machine mnemonic table and the pseudo table.
  98.  */
  99. struct oper *
  100. mne_look(str)
  101. char    *str;
  102. {
  103.     struct oper *low,*high,*mid;
  104.     int     cond;
  105.  
  106.     /* Search machine mnemonics first */
  107.     low =  &table[0];
  108.     high = &table[ NMNE-1 ];
  109.     while (low <= high){
  110.         mid = low + (high-low)/2;
  111.         if( ( cond = strcmp(str,mid->mnemonic)) < 0)
  112.             high = mid - 1;
  113.         else if (cond > 0)
  114.             low = mid + 1;
  115.         else
  116.             return(mid);
  117.     }
  118.  
  119.     /* Check for pseudo ops */
  120.     low =  &pseudo[0];
  121.     high = &pseudo[ NPSE-1 ];
  122.     while (low <= high){
  123.         mid = low + (high-low)/2;
  124.         if( ( cond = strcmp(str,mid->mnemonic)) < 0)
  125.             high = mid - 1;
  126.         else if (cond > 0)
  127.             low = mid + 1;
  128.         else
  129.             return(mid);
  130.     }
  131.  
  132.     return(NULL);
  133. }
  134.